#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int maxP = 0;
        if (prices.size() <= 1) return maxP;
        if (prices.size() ==2 ) { 
            if (prices[0] < prices[1]) return prices[1] - prices[0];
            else return 0;
        }   

        int curMin = 0, curMax = 0, i = 0;
        while (i < prices.size() -1 && prices[i] > prices[i-1]) i++;
        curMin = i;
        for ( ; i < prices.size()-1; i++) {
            if (prices[i] > prices[i+1]) {
                maxP += prices[i] - prices[curMin];;
                curMin = i+1;    
            }   
        }   
        if (prices[prices.size()-1] > prices[curMin]) 
            maxP += prices[prices.size()-1] - prices[curMin];
       // cout << maxP << endl;
        return maxP;
    }   
};


//add much simpleer code
    int maxProfit2(vector<int> &prices) {
        if (prices.size() < 2) return 0;
        int maxProfit = 0;
        for (int i = 1; i < prices.size(); i++) {
            if (prices[i] >= prices[i-1]) maxProfit += prices[i] - prices[i-1];  
        }
        return maxProfit;
    }


//pass all the test on 1337 point is to find the first start point at the beginning.
int main(int argc, char **argv)
{
    Solution mySolution;
    vector<int> prices;
    prices.push_back(1);
    prices.push_back(5);
    prices.push_back(2);
    prices.push_back(6);
    mySolution.maxProfit(prices);
}
